<?php

/**
 *  全局控制器
 * 
 */

class defaultController extends Controller
{
    function __construct()
    {
        parent::__construct();
        if(!isset($_SESSION['userid'])) {
            if(isset($_COOKIE['userid'])) {
                $_SESSION['userid'] = $_COOKIE['userid']; 
            }
        }

        if(!isset($_SESSION['username'])) {
            if(isset($_COOKIE['username'])) {
                $_SESSION['username'] = $_COOKIE['username']; 
            }
        }

    }   
    /**
     * 获得mysql当前时间
     * @return type
     */
    protected function getMysqlCurrentUnixTimestamp()
    {
        return $this ->_model('default') -> mysqlTime();
    }
    
    public function for_post($post){
        if(isset($_POST[$post])){
            return $_POST[$post];
        }else{
            return '';
        }
    }
    
    public function for_get($post){
        if(isset($_GET[$post])){
            return $_GET[$post];
        }else{
            return '';
        }
    }
    
    public function isSuperAdmin($id)
    {
        return !empty($id) && \in_array($id, $this -> getSuperAdmins()) ? true : false;
    }
    
    public function getUserId() 
    {
        return $_SESSION['userid'];
    }
    
    function checkLogin()
    {
        return (isset($_SESSION['userid']) && $_SESSION['userid'] > 0) ? true : false;
    }
    
    function checkadminLogin()
    {
        return (isset($_SESSION['adminid']) && $_SESSION['adminid'] > 0) ? true : false;
    }
    
    function logout()
    {
        session_start();
        session_destroy();
    }

    function response($data)
    {
        echo json_encode($data);
        exit;
    }

    function halt($data)
    {
       echo '<pre>'; print_r($data); exit;
    }

    function redirectPage($url)
    {
        header("Location: " . $url);
        exit;
    }
    
    /**
     * 提示信息页面跳转，跳转地址如果传入数组，页面会提示多个地址供用户选择，默认跳转地址为数组的第一个值，时间为5秒。
     * showmessage('登录成功', array('默认跳转地址'=>'http://www.phpcms.cn'));
     * @param string $msg 提示信息
     * @param mixed(string/array) $url_forward 跳转地址
     * @param int $ms 跳转等待时间
     */
    public function showMessage($msg, $url_forward = 'goback', $ms = 22250) {
        $this -> _view('message.php', array('msg' => $msg, 'url_forward' => $url_forward, 'ms' => $ms));
        exit;
    }

    /**
     * 获取请求ip
     *
     * @return ip地址
     */
    public function ip() {
        if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $ip = getenv('REMOTE_ADDR');
        } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
    }

    /**
     *
     */
    public function get_full_url()
    {
        $https = !empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'on') === 0;
        return
            ($https ? 'https://' : 'http://').
            (!empty($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : '').
            (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].
            ($https && $_SERVER['SERVER_PORT'] === 443 ||
            $_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
            substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
    }

    /**
     *
     */
    public function get_server_var($id)
    {
        return isset($_SERVER[$id]) ? $_SERVER[$id] : '';
    }


    public function generate_response($data)
    {
        return json_encode($data);
    }

}
 ?>